import yaml
from .basic_sate_form import basic_sate_form

class simple_sate_form(basic_sate_form):
    '''
    简易卫星编队，仅包含编队姿态信息
    '''
    def __init__(self, yaml_config):
        self.__angular_velocity = yaml_config.get("AngularVelocity", 1.0) * 60. * 24.    # 队列轴向的变换角速度 deg/min

        self.__a_year = 365.2422    # 一年对应的天数 day
        self.__orbit_rotate_angular_velocity = 360. / self.__a_year # 卫星在L2点绕日转动的角速度 deg/day
        self.__orbit_lon = yaml_config.get("InitialOrbitLongitude", 0.)     # 队列初始位置黄道经度 deg
        self.__orbit_lat = yaml_config.get("InitialOrbitLatitude", 0.)      # 队列初始位置黄道纬度 deg

        self.__point_lon = yaml_config.get("InitialPointingLongitude", 0.)  # 队列初始指向黄道经度 deg
        self.__point_lat = yaml_config.get("InitialPointingLatitude", 0.)   # 队列初始指向黄道纬度 deg

        self.__visual_angle = yaml_config.get("VisualAngle", 45.)   # 可视角度 deg
        self.__sleep_time   = yaml_config.get("SleepTime", 1.)          # 无观测目标时的等待时间 day

    def update_satellite(self, current_target=[], total_time_cost = 0., sleep = False):
        if sleep == True:
            #self.__orbit_lon = self.__orbit_lon + self.__sleep_time * self.__orbit_rotate_angular_velocity
            self.update_orbit_lon(self.__sleep_time * self.__orbit_rotate_angular_velocity)
            return -1, self.__sleep_time

        # 更新卫星编队状态
        self.__point_lon = current_target[1]
        self.__point_lat = current_target[2]
        #self.__orbit_lon = self.__orbit_lon + total_time_cost * self.__orbit_rotate_angular_velocity
        self.update_orbit_lon(total_time_cost * self.__orbit_rotate_angular_velocity)

        return current_target[0], total_time_cost

    def update_orbit_lon(self, delta_lon):
        self.__orbit_lon = self.__orbit_lon + delta_lon
        if self.__orbit_lon > 360.:
            self.__orbit_lon -= 360.

    def end_operation(self):
        pass

    @property
    def angular_velocity(self):
        return self.__angular_velocity

    @property
    def orbit_rotate_angular_velocity(self):
        return self.__orbit_rotate_angular_velocity

    @property
    def visual_angle(self):
        return self.__visual_angle

    @property
    def sleep_time(self):
        return self.__sleep_time

    @property
    def orbit_lon(self):
        return self.__orbit_lon

    @orbit_lon.setter
    def orbit_lon(self, lon):
        if lon > 360.:
            lon = lon - 360.
        self.__orbit_lon = lon

    @property
    def orbit_lat(self):
        return self.__orbit_lat

    @orbit_lat.setter
    def orbit_lat(self, lat):
        self.__orbit_lat = lat

    @property
    def point_lon(self):
        return self.__point_lon

    @point_lon.setter
    def point_lon(self, lon):
        if lon > 360.:
            lon = lon - 360.
        elif lon < 0.:
            lon = 360. + lon
        self.__point_lon = lon

    @property
    def point_lat(self):
        return self.__point_lat

    @point_lat.setter
    def point_lat(self, lat):
        self.__point_lat = lat